package com.serkol.ams.controller.api;

import cn.hutool.core.date.DateUtil;
import com.serkol.ams.bean.Result;
import com.serkol.ams.service.*;
import com.serkol.annotation.IsLogin;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/stastica")
public class StasticaApiController {
    @Autowired
    private AgentService agentService;
    @Autowired
    private OrderMasterService masterService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UsersWalletLogService walletLogService;
    @Autowired
    private UsersService userService;

    @Autowired
    private UsersWalletService usersWalletService;
    @Autowired
    private UsersWalletWithdrawalService usersWalletWithdrawalService;
    @Autowired
    private LoginLogService loginLogService;


    @Autowired
    private GoodsService goodsService;

    @IsLogin
    @GetMapping("/getMyStastica")
    public Result getMyStastica(@RequestParam(value = "startDate", defaultValue = "") String startDate, @RequestParam(value = "endDate", defaultValue = "") String endDate) {
        System.out.println(startDate);

        Date startDate1 = DateUtils.addMonths(new Date(), -6);

        Date endDate1 = new Date();

        Map<String, Object> order = masterService.myOrderStastica(startDate1, endDate1);
        Map<String, Object> teamOrder = masterService.teamOrderStastica(startDate1, endDate1);

        Map<String, Object> wallet = walletLogService.myWalletLogStastica(startDate1, endDate1);
        Map<String, Object> teamWallet = walletLogService.teamWalletLogStastica(startDate1, endDate1);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("order", order);
        map.put("wallet", wallet);
        map.put("teamOrder", teamOrder);
        map.put("teamWallet", teamWallet);

        return Result.ok(map);
    }

    //SUM(PRICE) as value,MONTH(CREATE_DATE) as timePeriod
    @IsLogin
    @GetMapping("/myWalletStastica")
    public Result getMyStastica(@RequestParam(value = "userId", defaultValue = "0") long userId) {
        if (userId > 0) {
            Map<String, Object> wallet = masterService.myWalletStastica(userId);
          //  Map<String, Object> wallet = walletLogService.myWalletStastica(userId);

            return Result.ok(wallet);
        } else {
            return Result.failure();
        }
    }

    @IsLogin
    @GetMapping("/myWalletStasticaOval")
    public Result getMyStasticaOval() {

        List<Map<String, Object>> wallet = walletLogService.myWalletStasticaOval();

        return Result.ok(wallet);
    }


    //  @IsLogin
    @GetMapping("/financeChartStastic")
    public Result financeChartStastic() {

        List<Map<String, Object>> data = walletLogService.financeChartStastic();

        return Result.ok(data);
    }

    @GetMapping("/loginChartStastic")
    public Result loginChartStastic() {

        List<Map<String, Object>> data = loginLogService.loginChartStastic();

        return Result.ok(data);
    }

   // @Cacheable(value = "stastica", key = "'homeChartStastic'")
    @GetMapping("/homeChartStastic")
    public Result homeChartStastic() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> user = userService.getUserStastic();
        Map<String, Object> order = masterService.getUsersStastic();
        Map<String, Object> wallet = usersWalletService.getUsersStastic();
        Map<String, Object> withdrawal = usersWalletWithdrawalService.getUsersStastic();

        map.put("user", user);
        map.put("order", order);
        map.put("wallet", wallet);
        map.put("withdrawal", withdrawal);
        return Result.ok(map);
    }

    @GetMapping("/homeOrderChartStastic")
    public Result homeOrderChartStastic() {

        List<Map<String, Object>> data = masterService.homeOrderChartStastic();

        return Result.ok(data);
    }

    @GetMapping("/homeAgentChartStastic")
    public Result homeAgentChartStastic() {

        List<Map<String, Object>> data = agentService.homeAgentChartStastic();

        return Result.ok(data);
    }
    @GetMapping("/homeGoodsChartStastic")
    public Result homeGoodsChartStastic(@RequestParam(value = "type", defaultValue = "0") int type) {

        List<Map<String, Object>> data = orderDetailService.homeGoodsChartStastic(type);

        return Result.ok(data);
    }

}
